'use client';

import { queryMenuDetails } from '@/services/api';
import { useQuery } from '@tanstack/react-query';
import type { IMenu } from '@/interfaces';
import { useContext } from 'react';
import Alert from '@/app/[locale]/alert/alert';
import { MenuAdminContext } from '@/contexts/admin/menu';
import AlertLoad from '@/app/[locale]/alert/load';
import QueryMenuInfo from '@/app/[locale]/admin/menu/detail/info';
import QueryMenuSubmenu from '@/app/[locale]/admin/menu/detail/submenu';

export default function DetailsMenuAdminPage() {
  const context = useContext(MenuAdminContext);

  const queryMenuDetailsQuery = useQuery(
    ['/menus', context.detailItem?.id],
    async (context) => {
      return (await queryMenuDetails({
        id: context.queryKey[1] + '',
      })) as IMenu;
    },
  );

  if (queryMenuDetailsQuery.data) {
    const data = queryMenuDetailsQuery.data;

    return (
      <div className="row">
        <div className="col">
          <div className="vstack gap-4">
            <div className="row">
              <div className="col">
                <QueryMenuInfo details={data} />
              </div>

              <QueryMenuSubmenu details={data} />
            </div>

            <div></div>
          </div>
        </div>
      </div>
    );
  }

  if (queryMenuDetailsQuery.error) {
    return <Alert type="error" message={queryMenuDetailsQuery.error} />;
  }

  return <AlertLoad />;
}
